package com.le.tester.javaThink.fourth.concurrency;

import com.le.tester.javaThink.fourth.net.mindview.util.DaemonThreadFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * createTime：2021/9/13 13:33
 * description：创建守护线程
 */
public class DaemonFromFactory implements Runnable {

    @Override
    public void run() {
        try {
            while (true) {
                TimeUnit.MILLISECONDS.sleep(100);
                System.out.println(Thread.currentThread() + " " + this);
            }
        } catch (Exception e) {
            System.out.println("Interrupted" + e.getMessage());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newCachedThreadPool(new DaemonThreadFactory());
        for (int i = 0; i < 10; i++)
            service.execute(new DaemonFromFactory());
        System.out.println("all daemon started");
        TimeUnit.MILLISECONDS.sleep(500);
    }
}
